package com.example.aaa;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class DetailActivity extends AppCompatActivity implements View.OnClickListener {
    private Button back;
    private Button save;
    private EditText title;
    private EditText context;
    private TextView BG_content;
    private TextView FN_content;
    private int note_id = 0;
    private String get_title;//获取标题字符串
    private String get_context;//获取内容字符串
    private Spinner sp_dropdown;
    private String strBGDate;
    private String strFNDate;
    private String strFNTime;
    private String strBGTime;
    private String strBG;
    private String strFN;
    private String statu;
    private String[] learingSiteArrar = {"学习", "工作", "日常", "生活", "社交", "购物"};
    private int Hour;
    private int Minute;
    private String get_statu;
    private ImageView BGTime;//开始时间
    private ImageView FNTime;//结束时间
    private String get_BG_Time;
    private String get_FN_Time;
    private AdapterView.OnItemSelectedListener onItemSelectedListener = new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int postion, long I) {
            switch (adapterView.getId()) {
                case R.id.sp_dropdown_2:
                    statu = postion + "";
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        title = (EditText) findViewById(R.id.title_detail);
        context = (EditText) findViewById(R.id.context_detail);
        BG_content = (TextView) findViewById(R.id.BG_Time_content);
        FN_content = (TextView) findViewById(R.id.FN_Time_content);
        back = (Button) findViewById(R.id.back_detail);
        save = (Button) findViewById(R.id.save_detail);
        BGTime = (ImageView) findViewById(R.id.BG_Time_Right_jv);
        FNTime = (ImageView) findViewById(R.id.FN_Time_Right_jv);
        sp_dropdown = (Spinner) findViewById(R.id.sp_dropdown_2);

        back.setOnClickListener(this);
        save.setOnClickListener(this);
        BGTime.setOnClickListener(this);
        FNTime.setOnClickListener(this);

        //接收listView中点击item传来的note_id,
        Intent intent = getIntent();
        note_id = intent.getIntExtra("note_id", 0);
        NoteOperator noteOperator = new NoteOperator(this);
        Note note = noteOperator.getNoteById(note_id);

        title.setText(String.valueOf(note.title));
        context.setText(String.valueOf(note.context));
        BG_content.setText(String.valueOf(note.createTime));
        FN_content.setText(String.valueOf(note.finishTime));
        sp_dropdown.setSelection(Integer.parseInt(note.statu));//此处存疑


        intiDropdownSpinner(Integer.parseInt(note.statu));

    }

    private void intiDropdownSpinner(int statu) {
        Spinner sp_dropdown = (Spinner) findViewById(R.id.sp_dropdown_2);


        ArrayAdapter<String> learningSiteAdapter = new ArrayAdapter<String>(this,
                R.layout.item_select, learingSiteArrar);

        sp_dropdown.setAdapter(learningSiteAdapter);
        sp_dropdown.setSelection(statu);
        sp_dropdown.setOnItemSelectedListener(onItemSelectedListener);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_detail: {
                Intent intent = new Intent(DetailActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
            break;
            case R.id.save_detail: {
                get_title = title.getText().toString().trim();
                get_context = context.getText().toString().trim();
                get_statu = statu;
                if (TextUtils.isEmpty(get_title) || TextUtils.isEmpty(get_context)) {
                    Toast.makeText(this, "修改内容不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    Note note = new Note();
                    note.note_id = note_id;
                    note.title = get_title;
                    note.context = get_context;
                    note.createTime = BG_content.toString();
                    note.finishTime = FN_content.toString();
                    note.statu = statu;
                    NoteOperator noteOperator = new NoteOperator(DetailActivity.this);
                    noteOperator.update(note);

                    Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setClass(DetailActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }

            }
            break;

            case R.id.BG_Time_Right_jv: {

                strBGDate = null;
                strBGTime = null;
                strBG = "";
                Calendar cal = Calendar.getInstance();
                int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);    //得到小时
                int minute = cal.get(Calendar.MINUTE);            //得到分钟
                new TimePickerDialog(DetailActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        Hour = hourOfDay;
                        Minute = minute;
                        strBGTime = " " + hourOfDay + ":" + minute;
                        if (strBGDate != null && strBGTime != null) {
                            strBG = strBGDate + strBGTime;

                        } else {
                            strBG = "";
                        }
                        BG_content.setText(strBG);
                    }
                }, hourOfDay, minute, true).show();


                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);
                new DatePickerDialog(DetailActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                        strBGDate = year + "/" + (month + 1) + "/" + day + "";

                    }
                }, year, month, day).show();


            }
            break;

            case R.id.FN_Time_Right_jv: {

                strFNDate = null;
                strFNTime = null;
                strFN = null;

                Calendar cal = Calendar.getInstance();
                int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);    //得到小时
                int minute = cal.get(Calendar.MINUTE);            //得到分钟
                new TimePickerDialog(DetailActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        strFNTime = " " + hourOfDay + ":" + minute;
                        if (strFNDate != null && strFNTime != null) {
                            strFN = strFNDate + strFNTime;

                        } else {
                            strFN = "";
                        }
                        FN_content.setText(strFN);
                    }
                }, hourOfDay, minute, true).show();


                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);
                new DatePickerDialog(DetailActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                        strFNDate = year + "/" + (month + 1) + "/" + day + "";

                    }
                }, year, month, day).show();


            }
            break;


        }

    }
}
